#include <algorithm>
#include <stdexcept>

#include "DemoContainerCommon.h"
#include "DemoContainer.h"

DemoContainer::DemoContainer()
{}

DemoContainer::~DemoContainer()
{}

int DemoContainer::get_size( void )
{
  int ret = 0;

  ret = Container_.size();

  return ret;
}

int DemoContainer::add_item( const std::string& str )
{
  int ret = -1;
  bool ret_find = false;

  for ( std::vector<std::string>::size_type i = 0; i < Container_.size(); i++ )
  {
    if ( Container_[i] == str )
    {
      ret_find = true;
      break;
    }
  }

  if ( !ret_find )
  {
    Container_.push_back(str);
    ret = Container_.size() - 1;
  }

  return ret;
}

std::string DemoContainer::get_item( int index )
{

  if ( index < 0  
       || (std::vector<std::string>::size_type)index >= Container_.size() )
  {
    throw "out_of_range";
  }

  return Container_.at(index);
}

bool DemoContainer::remove_item( int index )
{
  bool ret = false;

  if ( ( index >= 0 ) 
    && ( ((std::vector<std::string>::size_type)index) < Container_.size() ) )
  {
    std::vector<std::string>::iterator iter =
      std::find(Container_.begin(), Container_.end(), Container_[index]);

    Container_.erase(iter);
    ret = true;
  }

  return ret;
}
